﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc; 
using System.ComponentModel;
using System.Collections;

namespace SRF.UI.Html
{
    public static class ButtonAndLinkExtensions
    {
        public static MvcHtmlString Button(this HtmlHelper htmlHelper, string value = null, string action = null, object htmlAttributes = null)
        {
            if (!string.IsNullOrEmpty(action) && !SRF.Security.SecurityContext.HasActionGrant(action))
            {
                return MvcHtmlString.Empty;
            }
            else
            {
                var attrs = new Dictionary<string, object>();

                if (htmlAttributes != null)
                {
                    foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(htmlAttributes))
                    {
                        attrs.Add(property.Name.Replace('_', '-'), property.GetValue(htmlAttributes));
                    }
                }

                TagBuilder tagBuilder = new TagBuilder("input");
                tagBuilder.MergeAttribute("type", "button");
                tagBuilder.MergeAttributes(attrs, true);
                if (value != null)
                    tagBuilder.MergeAttribute("value", value);

                return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));
            }
        }

        public static MvcHtmlString HyperLink(this HtmlHelper htmlHelper, string href = null, string text = null, string action = null, object htmlAttributes = null)
        {
            if (!string.IsNullOrEmpty(action) && !SRF.Security.SecurityContext.HasActionGrant(action))
            {
                return MvcHtmlString.Empty;
            }
            else
            {
                var attrs = new Dictionary<string, object>();

                if (htmlAttributes != null)
                {
                    foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(htmlAttributes))
                    {
                        attrs.Add(property.Name.Replace('_', '-'), property.GetValue(htmlAttributes));
                    }
                }

                TagBuilder tagBuilder = new TagBuilder("a");
                tagBuilder.MergeAttributes(attrs);
                tagBuilder.SetInnerText(text);
                if (href != null)
                    tagBuilder.MergeAttribute("href", href);

                return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
            }
        }
    }
}